<template>
  <!-- 用了一个行列布局 -->
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%;"
  >
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex">
        <!-- 两个内容 -->
        <el-col :span="12">{{ nodeData.manager }}</el-col>
        <el-col :span="12">
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleCommand">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">修改部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { reqDelDepartments } from '@/api/departments'
// css中  background-color
// js中  style.backgroundColor

// html中 :node-data="值"
// js中   props: { nodeData: xxx }
export default {
  props: {
    // 定义传入的属性
    nodeData: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async handleCommand(type) {
      if (type === 'add') {
        // console.log('添加操作')
        // 点击添加按钮的同时，需要传递nodeData，当前节点的信息
        this.$emit('add-depts', this.nodeData)
      }

      if (type === 'edit') {
        // console.log('编辑操作')
        this.$emit('edit-depts', this.nodeData)
      }

      if (type === 'del') {
        // console.log('删除操作')
        // 发送请求
        await reqDelDepartments(this.nodeData.id)
        // 添加提示
        this.$message.success('恭喜删除成功')
        // 重新渲染，通知老爹，重新获取一下数据
        this.$emit('del-depts')
      }
    }
  }
}
</script>

<style>

</style>
